﻿<template>
  <div>
    <!-- 收货单 -->
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('WmsReceiptOrder:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="单据编号">
                <a-input v-model="queryParam.no" allow-clear placeholder="请输入单据编号" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="送货通知单号">
                <a-input v-model="queryParam.sourceBillno" allow-clear placeholder="请输入送货通知单号" />
              </a-form-item>
            </a-col><template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="供应商编码">
                  <a-input v-model="queryParam.vendorcode" allow-clear placeholder="请输入供应商编码" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="供应商名称">
                  <a-input v-model="queryParam.vendorname" allow-clear placeholder="请输入供应商名称" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="公司名称">
                  <a-input v-model="queryParam.companyname" allow-clear placeholder="请输入公司名称" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="仓管员">
                  <a-input v-model="queryParam.warehouseKeepername" allow-clear placeholder="请输入仓管员" />
                </a-form-item>
              </a-col><a-col :md="8" :sm="24">
                <a-form-item label="贸易方式">
                  <a-select :allowClear="true" style="width: 100%" v-model="queryParam.tradeMode" placeholder="请选择贸易方式">
                    <a-select-option v-for="(item, index) in tradeModeData" :key="index" :value="item.code">{{ item.name
                    }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="仓储中心">
                  <a-input v-model="queryParam.warehouseCentername" allow-clear placeholder="请输入仓储中心" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="仓间">
                  <a-input v-model="queryParam.storeRoomname" allow-clear placeholder="请输入仓间" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="报关单号">
                  <a-input v-model="queryParam.declarationCode" allow-clear placeholder="请输入报关单号" />
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="专业">
                  <a-input v-model="queryParam.majorname" allow-clear placeholder="请输入专业" />
                </a-form-item>
              </a-col><a-col :md="8" :sm="24">
                <a-form-item label="单据状态">
                  <a-select :allowClear="true" style="width: 100%" v-model="queryParam.orderStatus" placeholder="请选择单据状态">
                    <a-select-option v-for="(item, index) in orderStatusData" :key="index" :value="item.code">{{ item.name
                    }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col> </template>

            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px"> {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'" />
                </a>
              </span>
            </a-col>

          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <!-- :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" -->
      <s-table ref="table" :scroll="{ x: true }" :columns="columns" :data="loadData" :alert="true"
        :rowKey="(record) => record.id" >
        <template slot="index" slot-scope="text, record, index">{{ index + 1 }}</template>

        <template slot="linkSlot" slot-scope="text,record">
          <a @click="onOpenInfo(record)">{{ text }}</a>
        </template>

        <template class="table-operator" slot="operator" v-if="hasPerm('WmsReceiptOrder:add')">

          <a-button type="info" v-if="hasPerm('sysExcelTemplate:importExcel')" icon="upload" @click="$refs.excelForm.index()">
            导入
          </a-button>
          <a-button type="info" v-if="hasPerm('wmsReceiptOrder:exportExcel')" icon="download" @click="wmsReceiptOrderToExcel()">
            导出
          </a-button>
          <a-button type="primary" v-if="hasPerm('WmsReceiptOrder:add')" icon="plus" @click="$refs.addForm.add()">更新</a-button>
        </template>
        <span slot="tradeModescopedSlots" slot-scope="text">
          {{ 'trade_mode' | dictType(text) }}
        </span>
        <!-- <span slot="orderStatusscopedSlots" slot-scope="text">
          {{ 'order_statusenum' | dictType(text) }}
        </span> -->
        <span slot="action" slot-scope="text, record">
          <!-- <a v-if="hasPerm('WmsReceiptOrder:edit')" :class="[record.orderStatus == 4 ? 'disabled' : '']" @click="$refs.editForm.edit(record)">收货</a> -->
          <a v-if="hasPerm('WmsReceiptOrder:edit')" :class="[record.orderStatus == 4 ? 'disabled' : '']"
            @click="onDistribute(record)">收货</a>

            &nbsp;&nbsp;&nbsp;

            <a v-if="hasPerm('WmsReceiptOrder:edit')" :class="[record.receivingStatus == 1 ? 'disabled' : '']"
            @click="onDistributeBj(record)">报检</a>

          <!-- <a v-if="hasPerm('WmsReceiptOrder:edit')" @click="$refs.editForm.edit(record)">编辑</a> -->
          <!-- <a-divider type="vertical" v-if="hasPerm('WmsReceiptOrder:edit') & hasPerm('WmsReceiptOrder:delete')"/> -->
          <!-- <a-popconfirm v-if="hasPerm('WmsReceiptOrder:delete')" placement="topRight" title="确认删除？" @confirm="() => WmsReceiptOrderDelete(record)">
            <a>删除</a>
          </a-popconfirm> -->
        </span>

        <!-- <span slot="orderStatusscopedSlots" slot-scope="text">
          <a-tag :color="text == '2' ? '#daa520' : (text == '1' ? '#cd5c5c' : (text == '3' ? '#add8e6' : '#696969'))">{{ 'order_statusenum'
            | dictType(text) }}</a-tag>
        </span> -->


        <span slot="receivingStatusscopedSlots" slot-scope="text">
          <a-tag :color="text == '2' ? '#daa520' : (text == '1' ? '#cd5c5c' : (text == '3' ? '#add8e6' : '#696969'))">{{ 'ReceivingStatus'
            | dictType(text) }}</a-tag>
        </span>


        <span slot="inspectionStatusscopedSlots" slot-scope="text">
          <a-tag :color="text == '2' ? '#daa520' : (text == '1' ? '#cd5c5c' : (text == '3' ? '#add8e6' : '#696969'))">{{ 'InspectionStatus'
            | dictType(text) }}</a-tag>
        </span>



      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
      <detail-drawer ref="tableDetailDrawer" :visible.sync="infoVisible" :row="infoRow"  @postBjData="postBjData"/>
    </a-card>
  </div>
</template>
<script>
import { STable } from '@/components'
import { WmsReceiptOrderPage, WmsReceiptOrderDelete ,WmsReceiptOrderSh ,WmsOrderBaojian} from '@/api/modular/main/WmsReceiptOrderManage'
import addForm from './addForm.vue'
import editForm from './editForm.vue'
import DetailDrawer from './DetailDrawer.vue'

export default {
  components: {
    STable,
    addForm,
    editForm,
    DetailDrawer
  },
  data() {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},
      columns: [
      {
          dataIndex: 'index',
          title: '序号',
          fixed: 'left',
          width: '60',
          scopedSlots: { customRender: 'index' },
          align: 'center'
        },
        {
          title: '单据编号',
          align: 'center',
          fixed: 'left',
          dataIndex: 'no',
          scopedSlots: { customRender: 'linkSlot' },
          width: "140px"
        },
        // {
        //   title: '单据状态',
        //   align: 'center',
        //   dataIndex: 'orderStatus',
        //   scopedSlots: { customRender: 'orderStatusscopedSlots' },
        //   width: "100px"
        // },

        {
          title: '收货状态',
          align: 'center',
          dataIndex: 'receivingStatus',
          scopedSlots: { customRender: 'receivingStatusscopedSlots' },
          width: "100px"
        },
        {
          title: '报检状态',
          align: 'center',
          dataIndex: 'inspectionStatus',
          scopedSlots: { customRender: 'inspectionStatusscopedSlots' },
          width: "100px"
        },

        {
          title: '创建日期',
          align: 'center',
          dataIndex: 'billdate',
          width: "160px"
        },
        {
          title: '送货通知单号',
          align: 'center',
          dataIndex: 'sourceBillNo',
          width: "160px"
        },
        {
          title: '供应商编码',
          align: 'center',
          dataIndex: 'vendorcode',
          width: "160px"
        },
        {
          title: '供应商名称',
          align: 'center',
          dataIndex: 'vendorname',
          width: "160px"
        },
        {
          title: '公司名称',
          align: 'center',
          dataIndex: 'companyname',
          width: "160px"
        },
        {
          title: '仓管员',
          align: 'center',
          dataIndex: 'warehouseKeepername',
          width: "100px"
        },
        {
          title: '贸易方式',
          align: 'center',
          dataIndex: 'tradeMode',
          scopedSlots: { customRender: 'tradeModescopedSlots' },
          width: "100px"
        },
        {
          title: '仓储中心',
          align: 'center',
          dataIndex: 'warehouseCentername',
          width: "160px"
        },
        {
          title: '仓间',
          align: 'center',
          dataIndex: 'storeRoomname',
          width: "100px"
        },
        {
          title: '报关单号',
          align: 'center',
          dataIndex: 'declarationCode',
          width: "100px"
        },
        {
          title: '专业',
          align: 'center',
          dataIndex: 'majorname',
          width: "100px"
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remarks',
          width: "100px"
        },

      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
       
        return WmsReceiptOrderPage(Object.assign(parameter, this.queryParam)).then((res) => {

          this.$nextTick(()=>{
            if(res.data.rows && res.data.rows.length>0){
              this.onOpenInfo(res.data.rows[0]);
            }
          })
// // 测试数据
//           res.data.rows.forEach(element => {
//             element.receivingStatus =1;
//             element.inspectionStatus =1;
//           });
          return res.data
        })
      },
      tradeModeData: [],
      orderStatusData: [],
      selectedRowKeys: [],
      selectedRows: [],
      //详情变量
      infoRow: {},
      infoVisible: false,
      postBjDataArr: []
    }
  },
  created() {
    if (this.hasPerm('WmsReceiptOrder:edit') || this.hasPerm('WmsReceiptOrder:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        fixed: 'right',
        scopedSlots: { customRender: 'action' }
      })
    }
    const tradeModeOption = this.$options
    this.tradeModeData = tradeModeOption.filters['dictData']('trade_mode')
    const orderStatusOption = this.$options
    this.orderStatusData = orderStatusOption.filters['dictData']('order_statusenum')

  },
  methods: {
    /**
     * 查询参数组装
     */
    switchingDate() {
      const obj = JSON.parse(JSON.stringify(this.queryParam))
      return obj
    },
    WmsReceiptOrderDelete(record) {
      WmsReceiptOrderDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.refresh()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    //详情
    onOpenInfo(row) {
      this.infoVisible = true;
      this.infoRow = row;
      if(row){
       this.$refs.tableDetailDrawer.getList(row);
      }
    },

// 收货
    onDistribute(obj) {
      // if (obj.orderStatus !== 1 && obj.orderStatus !== 4) return false
      // if (obj.orderStatus == 1) return false

      if(this.postBjDataArr.length<=0 || obj.id != this.postBjDataArr[0].orderId){
        this.$message.error('请选中此单据下的物料!')
        return
      }
      this.$confirm({
        title: '系统提示',
        content: '您将要进行收货任务的操作，确认要继续嘛？',
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          let param = {
            "no":obj.no,
            getOrderDetailOutputList: this.postBjDataArr
          }
          WmsReceiptOrderSh(param).then(res => {
            if (res.success) {
              this.$message.success('操作成功')
              this.$refs.table.refresh()
              this.$refs.tableDetailDrawer.clearAll()
            } else {
              this.$message.error('操作失败')
            }
          })
        }
      })
    },

      // 选中的物料
      postBjData(data) {
      this.postBjDataArr = data
    },
// 报检
    onDistributeBj(obj) {
      // if (obj.orderStatus !== 1 && obj.orderStatus !== 4) return false
      // if (obj.orderStatus == 1) return false
      if(this.postBjDataArr.length<=0 || obj.id != this.postBjDataArr[0].orderId){
        this.$message.error('请选中此单据下的物料!')
        return
      }
      this.$confirm({
        title: '系统提示',
        content: '您将要进行报检任务的操作，确认要继续嘛？',
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          let param = {
            ...obj,
            dtls: this.postBjDataArr
          }
          WmsOrderBaojian(param).then(res => {
            if (res.success) {
              this.$message.success('操作成功')
              this.$refs.table.refresh()
            } else {
              this.$message.error('操作失败')
            }
          })
        }
      })
    },

  }
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
